<template>
  <div class="app-container" v-loading="loading">
    <div class="action-container">
      <el-button v-show="!multipleSelectionMode" icon="el-icon-plus" size="small" type="primary"
                 @click="openCreatedDialog">添加区域
      </el-button>
      <!--<el-button v-show="!multipleSelectionMode" icon="el-icon-delete" size="small"-->
      <!--@click="multipleSelectionMode=!multipleSelectionMode">批量删除-->
      <!--</el-button>-->
      <el-button type="danger" v-show="multipleSelectionMode" icon="el-icon-delete" size="small"
                 @click="deleteprinterAreas">删除
      </el-button>
      <el-button v-show="multipleSelectionMode" icon="el-icon-close" size="small" @click="closeMultipleSelect">取消
      </el-button>
      <el-button v-show="!multipleSelectionMode" icon="el-icon-refresh" size="small" @click="refresh">刷新</el-button>
    </div>

    <!--方块视图-->

    <!--<el-table
      :data="tableData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="printAreaName"
        label="区域名称">
      </el-table-column>
      <el-table-column
        prop="printerName"
        label="打印机名称">
      </el-table-column>
      <el-table-column
        label="编辑"
        align="left" min-width="90">
        <template slot-scope="scope">
          <el-button style="padding: 7px 27px;margin-left: 10px;margin-bottom: 3px" size="mini" type="primary"
                     icon="el-icon-edit" @click="openEditDialog(scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>-->
    <div class="printArea_box">
      <el-row>
        <el-col :xs="12" :sm="8" :md="6" :lg="4" :xl="4"
                class="area_square"
                v-for="(item,index) in tableData" :key="index">
          <div class="area_square_content">
            <div class="printAreaName">
              <p style="text-align: center" :title="'区域名:'+item.printAreaName">
                <span>{{item.printAreaName}}</span>
              </p>
            </div>
            <div class="printerName" :title="'打印机名:'+item.printerName">
              <span>打印机:{{item.printerName}}</span>
              <span :class="{hiddenEdit:multipleSelectionMode}" @click.prevent="openEditDialog(item)">编辑</span>
              <span @click="deleteprinterAreasSignle(item.printAreaId)">删除</span>

            </div>
            <p :class="{'no-select':multipleSelectionMode}">
              <span :class="{selectImg:item.active}" @click.stop="pitchon(item,item.printAreaId)"></span>
            </p>
          </div>
        </el-col>
      </el-row>
    </div>

    <!--分页-->
    <pagination
      :total-elements="totalElements"
      :change-callback="getprinterAreaList"
      ref="page"></pagination>
    <!--添加区域dialog-->
    <el-dialog
      title="添加区域"
      :visible.sync="createdDialog"
      :before-close="closeCreatedDialog">
      <el-form :model="form">
        <el-form-item label="区域名称:" :label-width="formLabelWidth">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="配置打印机:" :label-width="formLabelWidth">
          <el-select v-model="form.printerId" placeholder="请选择">
            <el-option
              v-for="item in printerOptions"
              :key="item.value"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeCreatedDialog">取 消</el-button>
        <el-button type="primary" @click="addprinterArea" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>


    <el-dialog
      title="编辑信息"
      :visible.sync="editDialog"
      :before-close="closeEditDialog">
      <el-form :model="currentRow">
        <el-form-item label="打印机选择" :label-width="formLabelWidth">
          <el-select v-model="currentRow.printerId" placeholder="请选择">
            <el-option
              v-for="item in printerOptions"
              :key="item.value"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区域名称" :label-width="formLabelWidth">
          <el-input v-model="currentRow.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeEditDialog">取 消</el-button>
        <el-button type="primary" @click="editprinterArea" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {printerSelectItem} from '../api/printer'
  import {
    printerAreaList,
    printerAreaAdd,
    printerAreaUpdate,
    printerAreaLogicalDel
  } from '../api/printerArea'
  import pagination from '@/components/pagination/index'
  import Vue from 'vue'

  export default {
    name: 'printerArea',
    data () {
      return {
        tableData: [], // 表格数据
        totalElements: 0, // 数据条数
        multipleSelection: [], // 选中数据-多选
        loading: true, // loading效果
        createdDialog: false, // 区域模态框
        formLabelWidth: '120px',
        printerOptions: [],
        query: {
          printerAreaRoomId: '',
          printerAreaTypeId: '',
        },
        specListForm: {
          list: [],
        },
        form: {
          name: '',
          printerId: '',
          foodIds: [],
        }, // 区域数据
        btnLoading: false, // 按钮loading
        editDialog: false, // 编辑模态框,
        currentRow: {
          id: '',
          name: '',
          printerId: '',
          foodIds: []
        },
        headers: {
          accessToken: sessionStorage.token
        }, // 请求头部

        multipleSelectionMode: false,
      }
    },
    components: {pagination},
    created () {
      this.getprinterAreaList()
      this.getprinterAreaRoomSelectItem()
      //获取店铺ID
      this.form.storeId = sessionStorage.getItem('storeId')
      this.currentRow.storeId = sessionStorage.getItem('storeId')
    },
    /*
      * 侦听是否开始批量删除模式，给出提示
      * */
    watch: {
      multipleSelectionMode: function (nv, ov) {
        if (nv) {
          this.$message({
            message: '进入批量删除模式，请谨慎操作。',
            type: 'warning'
          })
        }
      }
    },
    methods: {
      handleSelectionChange (val) {
        this.multipleSelection = val
      },
      handleDelete (row) {
        this.multipleSelection.push(row)
        this.deleteprinterAreas()
      },

      //**************************
      /*
      * 多选判断，选中与取消
      * */
      pitchon: function (it, id) {
        if (it.active) {
          Vue.set(it, 'active', false)  //为item添加不存在的属性，需要使用vue提供的Vue.set( object, key, value )方法
          for (let i = 0; i < this.multipleSelection.length; i++) {
            if (this.multipleSelection[i].printAreaId === id) {
              this.multipleSelection.splice(i, 1)
            }
          }
          // console.log(this.multipleSelection)
        } else {
          Vue.set(it, 'active', true)
          this.multipleSelection.push({printAreaId: id})
          // console.log(this.multipleSelection)
        }
      },

      /*
      * 取消多选
      * */
      closeMultipleSelect: function () {
        this.multipleSelectionMode = false
        this.multipleSelection = []
      }
      ,

      /*
      * 获取区域列表
      * */
      getprinterAreaList: function (number, pageSize) {
        this.loading = true
        printerAreaList(number, pageSize, this.query).then(response => {
          let data = response.obj
          console.log(data)
          this.totalElements = data.totalElements
          this.tableData = data.content
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      },
      /*
      * 刷新
      * */
      refresh () {
        this.$refs.page.refresh()
      },
      // 获取区域位置
      getprinterAreaRoomSelectItem: function () {
        printerSelectItem().then(response => {
          this.printerOptions = response.obj
        })
      },
      /*
      * 打开区域模态框
      * */
      openCreatedDialog () {
        this.createdDialog = true
        this.getprinterAreaRoomSelectItem()
      },
      /*
      * 关闭区域模态框
      * */
      closeCreatedDialog () {
        this.createdDialog = false
        /*this.form = {
          printeId: '',
          name: '',
        } // 区域数据*/
        this.form.name = ''
        this.form.printerId = ''// 区域数据
        this.chooseFoodList = []

      },
      /*
      * 添加区域
      * */
      addprinterArea () {
        this.loading = true
        this.btnLoading = true
        printerAreaAdd(this.form).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.closeCreatedDialog()
          this.getprinterAreaList()
        }).catch(() => {
          this.btnLoading = false
          this.loading = false
        })
      },
      /*
      * 打开编辑模态框
      * */
      openEditDialog (row) {
        this.currentRow.id = row.printAreaId
        this.currentRow.name = row.printAreaName
        this.currentRow.printerId = row.printerId
        this.editDialog = true
      },
      /*
      * 关闭编辑模态框
      * */
      closeEditDialog () {
        this.editDialog = false
      },
      /*
      * 编辑
      * */
      editprinterArea () {
        this.loading = true
        this.btnLoading = true
        printerAreaUpdate(this.currentRow).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.loading = false
          this.closeEditDialog()
          this.refresh()
        }).catch(() => {
          this.loading = false
          this.btnLoading = false
        })
      },
      /*
      * 删除区域
      * */
      deleteprinterAreas () {
        this.$confirm('是否删除这些区域？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (this.multipleSelection.length) {
            this.loading = true
            let ids = []
            this.multipleSelection.forEach((item) => {
              ids.push(item.printAreaId)
            })
            printerAreaLogicalDel(ids).then(response => {
              this.$message({
                message: response.msg,
                type: 'success',
                duration: 1000
              })
              this.multipleSelection = []
              this.$refs.page.refresh()
            }).catch(() => {
              this.loading = false
            })
          } else {
            this.$message({
              message: '请选择区域',
              type: 'warning',
              duration: 1000
            })
          }
        })
      },
      /*
     * 删除单个
     * */
      deleteprinterAreasSignle(id) {
        let ids = [id]
        this.$confirm('是否删除这个打印区域？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log(ids)
          printerAreaLogicalDel(ids).then(response => {
            this.$message({
              message: response.msg,
              type: 'success',
              duration: 1000
            })
            this.multipleSelection = []
            this.$refs.page.refresh()
          }).catch(() => {
            this.loading = false
          })
        })
      },

      /*
      * 搜索
      * */
      search () {
        this.$refs.page.search()
      },
    }
  }
</script>

<style scoped>
  .logo-uploader {
    display: inline-block;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .logo-uploader:hover {
    border-color: #409EFF;
  }

  .logo-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .logo {
    width: 178px;
    height: 178px;
    display: block;
  }

  /*方块视图*/

  .printArea_box {
    width: 100%;
    height: 100%;
    overflow: hidden;
    padding: 20px 0 20px 0;
    margin-left: -14px;
    margin-right: -14px;
  }

  .area_square {
    padding-left: 14px;
    padding-right: 14px;
  }

  .area_square_content {
    position: relative;
    margin-bottom: 16px;
    height: 98px;
    border-radius: 5px;
    background-color: white;
    border: 1px solid #d0d0d0;
  }

  .area_square :hover {
    cursor: pointer;
  }

  .printAreaName {
    width: 100%;
    height: 64px;
    line-height: 64px;
    margin-top: -17px;
    background-color: #409EFF;
    border-radius: 4px 4px 0 0;
  }

  .printAreaName span {
    color: white;
    font-size: 20px;
  }

  .printerName {
    position: relative;
    width: 100%;
    height: 34px;
    overflow: hidden;
  }

  .printerName span:nth-child(1) {
    position: absolute;
    top: 50%;
    font-size: 14px;
    color: #666666;
    margin-top: -10px;
    left: 15px;
    width: 170px;
    overflow: hidden
  }

  .printerName span:nth-child(2) {
    position: absolute;
    top: 50%;
    font-size: 14px;
    color: #409EFF;
    margin-top: -10px;
    right: 60px
  }

  .printerName span:nth-child(3) {
    position: absolute;
    top: 50%;
    font-size: 14px;
    color: #343434;
    margin-top: -10px;
    right: 15px
  }

  .no-select {
    position: absolute;
    top: -25px;
    right: -10px;
    width: 26px;
    height: 26px;
  }

  .no-select span {
    display: inline-block;
    width: 26px;
    height: 26px;
    background: url() no-repeat;
    background-size: 100% 100%;
  }

  .selectImg {
    background: url() no-repeat !important;
    background-size: 100% 100%;
  }

  .hiddenEdit {
    display: none;
  }


</style>
